<?php

namespace app\repository;

use app\popo\model\Dish;
use app\popo\model\Setmeal;
use app\popo\model\ShoppingCart;
use Carbon\Carbon;

class ShoppingCartRepository
{
    /**
     * 根据用户ID获取购物车列表
     * @param int $userId
     * @return array
     */
    public function getByUserId(int $userId): array
    {
        return ShoppingCart::where('user_id', $userId)
            ->orderBy('create_time', 'desc')
            ->get()
            ->map(function (ShoppingCart $shoppingCart) {
                return [
                    'amount' => $shoppingCart->amount,
                    'createTime' => $shoppingCart->create_time ? Carbon::parse($shoppingCart->create_time)->format("Y-m-d H:i:s") : null,
                    'dishFlavor' => $shoppingCart->dish_flavor,
                    'dishId' => $shoppingCart->dish_id,
                    'id' => $shoppingCart->id,
                    'image' => $shoppingCart->image ? get_full_url($shoppingCart->image) : null,
                    'name' => $shoppingCart->name,
                    'number' => $shoppingCart->number,
                    'setmealId' => $shoppingCart->setmeal_id,
                    'userId' => $shoppingCart->user_id,
                ];
            })
            ->toArray();
    }

    /**
     * 根据条件查找购物车项
     * @param array $conditions
     * @return ShoppingCart|null
     */
    public function findByConditions(array $conditions): ?ShoppingCart
    {
        $query = ShoppingCart::query();
        
        foreach ($conditions as $field => $value) {
            if ($value !== null) {
                $query->where($field, $value);
            }
        }
        
        return $query->first();
    }

    /**
     * 添加购物车项
     * @param array $data
     * @return void
     */
    public function add(array $data): void
    {
        $shoppingCart = new ShoppingCart();
        foreach ($data as $key => $value) {
            if (in_array($key, $shoppingCart->getFillable()) && $value !== null) {
                $shoppingCart->{$key} = $value;
            }
        }
        $shoppingCart->create_time = date('Y-m-d H:i:s');
        $shoppingCart->save();
    }

    /**
     * 更新购物车项数量
     * @param ShoppingCart $shoppingCart
     * @param int $number
     * @return void
     */
    public function updateNumber(ShoppingCart $shoppingCart, int $number): void
    {
        $shoppingCart->number = $number;
        $shoppingCart->save();
    }

    /**
     * 删除购物车项
     * @param ShoppingCart $shoppingCart
     * @return void
     */
    public function delete(ShoppingCart $shoppingCart): void
    {
        $shoppingCart->delete();
    }

    /**
     * 根据用户ID清空购物车
     * @param int $userId
     * @return void
     */
    public function cleanByUserId(int $userId): void
    {
        ShoppingCart::where('user_id', $userId)->delete();
    }

    /**
     * 根据菜品ID获取菜品信息
     * @param int $dishId
     * @return Dish|null
     */
    public function getDishById(int $dishId): ?Dish
    {
        return Dish::find($dishId);
    }

    /**
     * 根据套餐ID获取套餐信息
     * @param int $setmealId
     * @return Setmeal|null
     */
    public function getSetmealById(int $setmealId): ?Setmeal
    {
        return Setmeal::find($setmealId);
    }

}